<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * MAPI COA Project
 * home.php
 * @author ranuandra
 *
 * 22 Jul 2011
 * tags
 */

class Controller_Home extends Kohana_Mapi
{
	public $template = 'home';
	public function action_index()
	{
		
/*        try 
		{
			$_POST['username'] = "coba";
			$_POST['password'] = "12345678";
			$_POST['password_confirm'] = "12345678";
			$_POST['email'] = "adi@email.com";
			
			
			
			$user = ORM::factory('user');
			
			$user->create_user($_POST, array(
		 										'username',
												'password',
		 										'email',
		  									));
	  		$login_role = new Model_Role(array('name' =>'login'));
			$user->add('roles',$login_role);
	  		$login_role = new Model_Role(array('name' =>'admin'));
			$user->add('roles',$login_role);
			
		}
		catch (ORM_Validation_Exception $e) {
			$errors = $e->errors('models');
			print_r($errors);
		}*/
			
		if (Auth_ORM::instance()->logged_in() )
		{
			Request::current()->redirect(URL::base().'productspec');
			exit;

		}
		else 
		{
			$view = View::factory('login/form');
			$this->template->container = $view;	

		}	
	
		
	}
	
	public function action_login()
	{
		if(isset($_POST['username']) and isset($_POST['password']))
		{
			$user = Auth_ORM::instance();
			
			//echo $_POST['password'];
			//$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);
			//echo "dsadasd";
			//print_r($status);
			$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);

			if($status==1)
			{
				Request::current()->redirect(URL::base()."productspec");
			}
			else 
			{
				Request::current()->redirect(URL::base());
			}
			
			//print_r($status);
			//exit;
			//exit;
				
		}	
	}
	
	public function action_logout()
	{
		Auth_ORM::instance()->logout();
		Request::current()->redirect(URL::base());	
	}
	
}

?>